package sg.edu.nus.iss.team8s.vmcs.control;

import java.awt.Dimension;
import java.awt.TextField;
import java.awt.event.ActionListener;
import java.awt.event.TextListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Observable;
import java.util.Observer;

import sg.edu.nus.iss.team8s.vmcs.Listener.StoreViewerListener;
import sg.edu.nus.iss.team8s.vmcs.beans.Coin;
import sg.edu.nus.iss.team8s.vmcs.beans.DrinksStoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreItem;
import sg.edu.nus.iss.team8s.vmcs.beans.StoreObject;
import sg.edu.nus.iss.team8s.vmcs.builder.BuilderFactory;
import sg.edu.nus.iss.team8s.vmcs.builder.MachineryBuilder;
import sg.edu.nus.iss.team8s.vmcs.builder.UIBuilder;
import sg.edu.nus.iss.team8s.vmcs.builder.UIDirector;
import sg.edu.nus.iss.team8s.vmcs.cache.*;
import sg.edu.nus.iss.team8s.vmcs.gui.MachinerySimulatorPanel;
/**
 * 
 * @author Zheng Xiaofeng, Wang Shijie
 *
 */
public class MachineryController implements Observer {

	//private static final ActionListener StoreViewerListener = null;
	private MachinerySimulatorPanel msPanel;
	private TextField[] txtListCoinQty;
	private TextField[] txtListDrinkQty;
	
	public void update(Observable o, Object arg) {
		if(o instanceof DrinksStore){
			displayDrinkStock();
		}
		else if(o instanceof CashStore){
			displayCoinStock();
		}		
	}
	
	private static MachineryController instance = null;
	
	private MachineryController(){
		DrinksStore.getInstance().addObserver(this);
		CashStore.getInstance().addObserver(this);
	}
	
	public static synchronized MachineryController getInstance(){
		if(instance == null){
			instance = new MachineryController();
		}
		return instance;
	}

	public void displayDrinkStock(){
		//msPanel.getContentPanel().drinksStoreChange();
		/*if(msPanel != null){
			msPanel.removeAll();
			buildPanel();
			msPanel.validate();
		}*/
		if(txtListCoinQty == null)
			return;
		
		DrinksStore drinkStore = DrinksStore.getInstance();
		for(int i =0;i<drinkStore.getStoreSize();i++){
			StoreItem item = drinkStore.getItem(i);	
			TextListener[] txtListener = txtListDrinkQty[i].getListeners(TextListener.class);
			txtListDrinkQty[i].removeTextListener(txtListener[0]);
			txtListDrinkQty[i].setText(String.valueOf(item.getQuantity()));
			txtListDrinkQty[i].addTextListener(new StoreViewerListener(1,i,item.getQuantity()));
		}
	}
	
	public void displayCoinStock(){
		//msPanel.getContentPanel().cashStoreChange();\
		/*if(msPanel != null){
			msPanel.removeAll();
			buildPanel();
			msPanel.validate();
		}*/
		if(txtListCoinQty == null)
			return;
		CashStore cashStore = CashStore.getInstance();
		for(int i =0;i<cashStore.getStoreSize();i++){
			StoreItem item = cashStore.getItem(i);	
			TextListener[] txtListener = txtListCoinQty[i].getListeners(TextListener.class);
			txtListCoinQty[i].removeTextListener(txtListener[0]);
			txtListCoinQty[i].setText(String.valueOf(item.getQuantity()));
			txtListCoinQty[i].addTextListener(new StoreViewerListener(0,i,item.getQuantity()));
		}
	}
	
	public void displayMachineryPanel(final SimulationController simController){
		//msPanel = new MachinerySimulatorPanel();
		msPanel = MachinerySimulatorPanel.getInstance();
		msPanel.setSize(new Dimension(360,600));
		msPanel.setResizable(false);
		msPanel.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e) {
				simController.setActive(true,'3');
				msPanel.setVisible(false);
			}
		});
		
		buildPanel();
		msPanel.display();
	}
	
	private void buildPanel()
	{
		//UIBuilder builder = new MachineryBuilder();
		UIBuilder builder = BuilderFactory.getInstance().getUIBuilder("machineryUIBuilder");
		UIDirector uiDir = new UIDirector(builder);
		uiDir.build();
		msPanel.add(builder.getResult());
		msPanel.setContentPanel(builder.getResult());
	}
	
	public boolean giveChange(int index,int number_coins){
		StoreController storeController = StoreController.getInstance();
		return storeController.giveChange(index, number_coins);
	}
	
	public boolean storeCoin(Coin coin){
		StoreController storeController = StoreController.getInstance();
		return storeController.storeCoin(coin);
	}
	
	public boolean dispenseDrink(int selected_brand){		
		return StoreController.getInstance().dispenseDrink(selected_brand);
	}
	
	public void setCoinQtyList(TextField[] txtListCoinQty){
		this.txtListCoinQty = txtListCoinQty;
	}
	
	public void setDrinkQtyList(TextField[] txtListDrinkQty){
		this.txtListDrinkQty = txtListDrinkQty;
	}
}
